package com.ferris.redbook.util;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.HashMap;
import java.util.Map;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class AjaxResult {
    // 常用状态码常量
    public static final int SUCCESS_CODE = 200;
    public static final int BAD_REQUEST_CODE = 400;
    public static final int UNAUTHORIZED_CODE = 401;
    public static final int FORBIDDEN_CODE = 403;
    public static final int NOT_FOUND_CODE = 404;
    public static final int ERROR_CODE = 500;

    private Integer code;
    private String msg;
    private Object data;
    private Map<String, Object> extra;

    /**
     * 添加额外信息
     */
    public AjaxResult addExtra(String key, Object value) {
        if (this.extra == null) {
            this.extra = new HashMap<>();
        }
        this.extra.put(key, value);
        return this;
    }

    // 成功响应
    public static AjaxResult success() {
        return AjaxResult.builder()
                .code(SUCCESS_CODE)
                .msg("操作成功")
                .build();
    }

    public static AjaxResult success(String msg) {
        return AjaxResult.builder()
                .code(SUCCESS_CODE)
                .msg(msg)
                .build();
    }

    public static AjaxResult success(Object data) {
        return AjaxResult.builder()
                .code(SUCCESS_CODE)
                .msg("操作成功")
                .data(data)
                .build();
    }

    public static AjaxResult success(String msg, Object data) {
        return AjaxResult.builder()
                .code(SUCCESS_CODE)
                .msg(msg)
                .data(data)
                .build();
    }

    // 带分页信息的成功响应
    public static AjaxResult successWithPage(Object data, long total, int page, int limit) {
        Map<String, Object> pageData = new HashMap<>();
        pageData.put("list", data);
        pageData.put("total", total);
        pageData.put("page", page);
        pageData.put("limit", limit);

        return AjaxResult.builder()
                .code(SUCCESS_CODE)
                .msg("操作成功")
                .data(pageData)
                .build();
    }

    // 错误响应
    public static AjaxResult error(String msg) {
        return AjaxResult.builder()
                .code(ERROR_CODE)
                .msg(msg)
                .build();
    }

    public static AjaxResult error(Integer code, String msg) {
        return AjaxResult.builder()
                .code(code)
                .msg(msg)
                .build();
    }

    public static AjaxResult error(Integer code, String msg, Object data) {
        return AjaxResult.builder()
                .code(code)
                .msg(msg)
                .data(data)
                .build();
    }

    // 其他常见状态码
    public static AjaxResult unauthorized(String msg) {
        return error(UNAUTHORIZED_CODE, msg);
    }

    public static AjaxResult forbidden(String msg) {
        return error(FORBIDDEN_CODE, msg);
    }

    public static AjaxResult notFound(String msg) {
        return error(NOT_FOUND_CODE, msg);
    }

    public static AjaxResult badRequest(String msg) {
        return error(BAD_REQUEST_CODE, msg);
    }

    // 链式设置方法
    public AjaxResult withCode(Integer code) {
        this.code = code;
        return this;
    }

    public AjaxResult withMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public AjaxResult withData(Object data) {
        this.data = data;
        return this;
    }
}